<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
/**
 * Model wrapper for VideoInfo
 */
class Default_Model_VideoInfo extends At_Model{
	protected $_movieId;
	protected $_duration;
	protected $_contentDuration;
	protected $_videoFramerate;
	protected $_videoWidth;
	protected $_videoHeight;
	protected $_audioSamplerate;
	protected $_audioChannels;

	protected $mapper;
	protected $movie;

	/**
	 * set movieId. Asserts it being an int
	 * @param int $movieId
	 * @return $this fluent interface
	 */
	public function setMovieId( $id ){
		if( is_numeric( $id ) ){
			$this->_movieId = (int) $id;
		}
		return $this;
	}

	/**
	 * set the movie to use for retrieving data in this class
	 * @param Default_Model_Movie $movie
	 * @return $this fluent interface
	 */
	public function setMovie( Default_Model_Movie $movie ){
		$this->movie   = $movie;
		$this->_movieId = $movie->id;
		return $this;
	}

	/**
	 * fetches the movie
	 */
	public function getMovie(){
		return $this->movie;
	}

	/**
	 * fetch values from the database or try to get them from a file
	 */
	protected function find(){
		if( null !== $this->_movieId ){
			$this->getMapper()->find( $this );

			if( empty( $this->_duration ) ){
				$this->getInfo();
				if( ! empty( $this->_duration ) ){
					$this->save();
				}
			}
		}
	}

	public function save(){
		$this->getMapper()->save( $this );
	}

	/**
	 * fetch file statistics from oggz-info
	 */
	protected function getInfo(){
		$public = str_replace( 'application','public', APPLICATION_PATH );
		$path = $public . AT_MOVIE_PATH . '/' . $this->getMovie()->filename;
		$info = new Ogg_Info( $path );

		foreach( get_object_vars($this) as $key => $value ){
			// only _objectVars to be set.
			if( strpos( $key, '_' ) === 0 ){
				$property = ltrim( $key, '_' ); //str_replace( '_', '', $key );
				$this->__set( $property, $info->$property );
			}
		}

		return $this;
	}

    /**
     * Fetch or instantiates the mapper
     *
     * @return object default_model_mapper
     */
    protected function getMapper(){
        if( $this->mapper === null ){
            $mapperName = get_class( $this ) . 'Mapper';
            $this->setMapper( new $mapperName() );
        }

        return $this->mapper;
    }

    /**
     * Set mapper
     *
     * @param default_model_mapper $mapper
     */
    protected function setMapper( $mapper ){
        $this->mapper = $mapper;
        return $this;
    }
}
